#include <REGX52.H>
#include "Int1.h"
#include "Timer.h"

unsigned int IR_Time;
unsigned char IR_State;
unsigned char IR_Data[4];
unsigned char IR_pData;
unsigned char IR_DataFlag;
unsigned char IR_RepeatFlag;
unsigned char IR_Command;
unsigned char IR_Address;

void IR_Init()
{
    Timer0_Init();
    Int1_Init();
}

unsigned char IR_GetDataFlag()
{
    if(IR_DataFlag)
    {
        IR_DataFlag=0;
        return 1;
    }
    return 0;
}

unsigned char IR_GetRepeatFlag()
{
    if(IR_RepeatFlag)
    {
        IR_RepeatFlag=0;
        return 1;
    }
    return 0;
}

unsigned char IR_GetAddress()
{
    return IR_Address;
}

unsigned char IR_GetCommand()
{
    return IR_Command;
}

void Int1_Routine(void)	interrupt 2
{
    if(IR_State==0)
    {
        Timer0_SetCounter(0);
        Timer0_Run(1);
        IR_State=1;
    }
    else if(IR_State==1)
    {
        IR_Time=Timer0_GetCounter();
        Timer0_SetCounter(0);
        if (13000 < IR_Time && IR_Time < 14000)
        {
            IR_State=2;
        }
        else if (10750 < IR_Time && IR_Time < 11750)
        {
            IR_RepeatFlag=1;
            Timer0_Run(0);
            IR_State=0;
        }
        else{
            IR_State=1;
        }
    }
    else if(IR_State==2)
    {
        IR_Time = Timer0_GetCounter();
        Timer0_SetCounter(0);
        if (1120-500 < IR_Time && IR_Time < 1120+500)
        {
            IR_Data[IR_pData/8]&=~(0x01<<(IR_pData%8));
            IR_pData++;
        }
        else if (2250 - 500 < IR_Time && IR_Time < 2250 + 500)
        {
            IR_Data[IR_pData / 8] |= (0x01 << (IR_pData % 8));
            IR_pData++;
        }
        else{
            IR_pData=0;
            IR_State = 1;
        }
        if(IR_pData>=32)
        {
            IR_pData=0;
            if ((IR_Data[0] == ~IR_Data[1]) && (IR_Data[2] == ~IR_Data[3]))
            {
                IR_Address=IR_Data[0];
                IR_Command=IR_Data[2];
                IR_DataFlag = 1;
            }
            Timer0_Run(0);
            IR_State=0;
        }
    }
}